<?php 
namespace plugins\sms_account\controller; 

use cmf\controller\PluginAdminBaseController; //引入此类
use plugins\sms_account\model\SmsAccountModel;
use plugins\sms_account\model\SmsAccountBuyModel;
use plugins\sms_account\model\SmsAccountConsumeModel;
use think\Db;


/**
 * 短信帐号管理
 */
class SmsAccountController extends PluginAdminBaseController
{
	/**
	 *短信帐号管理
	 *@adminMenu(
	 *     'name'   => '短信帐号管理',
	 *     'parent' => 'AdminIndex/index',
	 *     'display'=> true,
	 *     'hasView'=> true,
	 *     'order'  => 1000,
	 *     'icon'   => '',
	 *     'remark' => '短信帐号管理',
	 *     'param'  => ''
	 * )
	 */
	public function index()
	{
		$param = $this->request->param();
		$search['condition1'] = 1;
		$search['keyword'] = '';
		if (isset($param['condition1'])) {
			$search['condition1'] = $param['condition1'];
		}
		if (isset($param['keyword'])) {
			$search['keyword'] = $param['keyword'];
		}

		$where = 1;
		if ($search['keyword']) {
			if ($search['condition1'] == 1) {
				$where .= " and `uid` =" . $search['keyword'];
			} elseif ($search['condition1'] == 2) {
				$where .= " and `account` like " . "'%" . $search['keyword'] . "%'";
			}
		}

		$accountList = SmsAccountModel::where($where)->paginate(20);

		$this->assign('page', $accountList->render()); //单独提取分页出来
		$this->assign('search', $search);
		$this->assign('account_list', $accountList);
		return $this->fetch('/sms_account/index');
	}




	/**
	 * 查看数据详情
	 *@adminMenu(
	 *     'name'   => '查看数据详情',
	 *     'parent' => 'SmsAccount/index',
	 *     'display'=> false,
	 *     'hasView'=> true,
	 *     'order'  => 1000,
	 *     'icon'   => '',
	 *     'remark' => '查看数据详情',
	 *     'param'  => ''
	 * )
	 */
	public function detail()
	{
		$param = $this->request->param();
		if (empty($param['id'])) {
			return zy_json_echo(false, '无id');
		}

		$id = $param['id'];
		$data = SmsAccountModel::where('id', $id)->find();

		$this->assign('data', $data);
		return $this->fetch('/sms_account/detail');
	}


	/**
	 * 修改数据资料
	 *@adminMenu(
	 *     'name'   => '修改数据',
	 *     'parent' => 'SmsAccount/index',
	 *     'display'=> false,
	 *     'hasView'=> false,
	 *     'order'  => 1000,
	 *     'icon'   => '',
	 *     'remark' => '修改数据',
	 *     'param'  => ''
	 * )
	 */
	public function edit()
	{
		$id = input('param.id');
		$data = SmsAccountModel::get($id);
		if ($this->request->isPost()) {
			$param = $this->request->param();
			// 验证器
			$result = $this->validate($param, 'SmsAccount.add');
			if (true !== $result) return zy_json_echo(false, $result);

			// 数据加入
			if (SmsAccountModel::update($param,'',true)) {
				return zy_json_echo(true, '修改成功');
			}
			return zy_json_echo(false, '修改失败');
		}
		$this->assign('data', $data);
		return $this->fetch('/sms_account/edit');
	}



	/**
	 * 添加数据
	 *@adminMenu(
	 *     'name'   => '添加数据',
	 *     'parent' => 'SmsAccount/index',
	 *     'display'=> false,
	 *     'hasView'=> true,
	 *     'order'  => 1000,
	 *     'icon'   => '',
	 *     'remark' => '添加数据',
	 *     'param'  => ''
	 * )
	 */	
	public function add(){
		if($this->request->isPost()){
			$param = $this->request->param();
			// 验证器
			$result = $this->validate($param, 'SmsAccount.add');
			if (true !== $result) return zy_json_echo(false, $result);

			// 数据加入
			if( SmsAccountModel::create($param, true) ){
				return zy_json_echo(true,'添加成功');
			}
			return zy_json_echo(false,'添加失败');
		}
		return $this->fetch('/sms_account/add');
	}



	/**
	 * 删除
	 *@adminMenu(
	 *     'name'   => '删除',
	 *     'parent' => 'SmsAccount/index',
	 *     'display'=> false,
	 *     'hasView'=> false,
	 *     'order'  => 1000,
	 *     'icon'   => '',
	 *     'remark' => '删除',
	 *     'param'  => ''
	 * )
	 */
	public function delete()
	{
		$param = $this->request->param();
		if (empty($param['id'])) {
			$this->error("传参错误");
		}
		$id = $param['id'];
		if (SmsAccountModel::destroy($id)) {
			$this->success("删除成功");
		}
		$this->error("删除失败");
	}



	/**
	 * 增加条数
	 *@adminMenu(
	 *     'name'   => '增加条数',
	 *     'parent' => 'SmsAccount/index',
	 *     'display'=> false,
	 *     'hasView'=> true,
	 *     'order'  => 1000,
	 *     'icon'   => '',
	 *     'remark' => '增加条数',
	 *     'param'  => ''
	 * )
	 */
	public function addNum()
	{
		$id = input('param.id');
		$data = SmsAccountModel::get($id);
		if ($this->request->isPost()) {
			$param = $this->request->param();
			// 验证器
			$result = $this->validate($param, 'SmsAccount.addNum');
			if (true !== $result) return zy_json_echo(false, $result);
			$buyData = $param;
			$buyData['accountid']= $buyData['id'];
			unset($buyData['id']);
			SmsAccountBuyModel::create($buyData, true);

			// 数据修改
			if ( SmsAccountModel::where('id', $param['id'])->setInc('total_num', $param['num']) ) {
				return zy_json_echo(true, '增加成功');
			}
			return zy_json_echo(false, '增加失败');
		}
		$this->assign('data', $data);
		return $this->fetch('/sms_account/addNum');
	}


}